package com.common;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Decription
 * Created by ucmed on 2017/12/20.
 */
public class BaseController {

    private static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    public boolean downloadPageByPost(String path) throws IOException {
        HttpClient httpClient = new HttpClient();
        //参数初始化
        NameValuePair[] postData = new NameValuePair[2];
        postData[0] = new NameValuePair("name", "lietu");
        postData[1] = new NameValuePair("password", "*****");
        //添加初始化到访问路径中
        PostMethod postMethod = new PostMethod(path);
        postMethod.addParameters(postData);
        //获取请求返回状态值
        int statusCode = httpClient.executeMethod(postMethod);
        //输出到文件
        try {
            return downloadPage(statusCode, postMethod, path);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            postMethod.releaseConnection();
        }
        return false;
    }

    public boolean downloadPageByGet(String path) throws IOException {
        HttpClient httpClient = new HttpClient();
        //添加初始化到访问路径中
        GetMethod getMethod = new GetMethod(path);
        //获取请求返回状态值
        int statusCode = httpClient.executeMethod(getMethod);
        //状态值为200的时候表示成功
        try {
            return downloadPage(statusCode, getMethod, path);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            getMethod.releaseConnection();
        }
        return false;
    }

    private boolean downloadPage(int statusCode, Object object, String path) throws IOException {
        //状态值为200的时候表示成功
        if (statusCode == HttpStatus.SC_OK) {
            InputStream inputStream = null;
            OutputStream outputStream = null;
            HttpMethod method = null;
            if (object instanceof PostMethod) {
                method = (PostMethod) object;
            } else if (object instanceof GetMethod) {
                method = (GetMethod) object;
            }
            inputStream = method.getResponseBodyAsStream();
            //获得文件名
            String fileName = path.substring(path.lastIndexOf('/') + 1);
            //获得文件输出流
            outputStream = new FileOutputStream(fileName);
            //输出到文件
            int tempByte = -1;
            while (0 < (tempByte = inputStream.read())) {
                outputStream.write(tempByte);
            }
            //关闭输出输入流
            if (null != inputStream) {
                inputStream.close();
            }
            if (null != outputStream) {
                outputStream.close();
            }
            method.releaseConnection();
            return true;
        }
        return false;
    }
}
